<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../../../../">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>selenium_driverless.scripts.network_interceptor &mdash; Selenium-Driverless 1.9.3.1 documentation</title>
      <link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css?v=80d5e7a1" />
      <link rel="stylesheet" type="text/css" href="../../../../_static/css/theme.css?v=19f00094" />

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../../../../_static/jquery.js?v=5d32c60e"></script>
        <script src="../../../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../../../../_static/documentation_options.js?v=58554080"></script>
        <script src="../../../../_static/doctools.js?v=888ff710"></script>
        <script src="../../../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../../../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../../../genindex/" />
    <link rel="search" title="Search" href="../../../../search/" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../../../../" class="icon icon-home">
            Selenium-Driverless
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/By/">By Element Locator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/Chrome/">webdriver.Chrome</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/ChromeOptions/">ChromeOptions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/Context/">Context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/Input/">Input</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/RequestInterception/">Request-Interception</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/Target/">Target</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api/WebELement/">WebElement</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../">Selenium-Driverless</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../../../" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../../../">Module code</a></li>
      <li class="breadcrumb-item active">selenium_driverless.scripts.network_interceptor</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for selenium_driverless.scripts.network_interceptor</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">typing</span>
<span class="kn">import</span> <span class="nn">uuid</span>
<span class="kn">from</span> <span class="nn">enum</span> <span class="kn">import</span> <span class="n">Enum</span>

<span class="kn">import</span> <span class="nn">aiohttp</span>
<span class="kn">from</span> <span class="nn">selenium_driverless.webdriver</span> <span class="kn">import</span> <span class="n">Chrome</span>
<span class="kn">from</span> <span class="nn">selenium_driverless.types.target</span> <span class="kn">import</span> <span class="n">Target</span>
<span class="kn">from</span> <span class="nn">selenium_driverless.types.base_target</span> <span class="kn">import</span> <span class="n">BaseTarget</span>
<span class="kn">import</span> <span class="nn">websockets</span>

<span class="kn">from</span> <span class="nn">cdp_socket.exceptions</span> <span class="kn">import</span> <span class="n">CDPError</span>

<span class="kn">import</span> <span class="nn">base64</span>

<span class="n">PatternsType</span> <span class="o">=</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]]</span>


<span class="c1"># TODO: support OrderedDict instead of List[Fetch.HeaderEntry]</span>


<div class="viewcode-block" id="RequestDoneException">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.RequestDoneException">[docs]</a>
<span class="k">class</span> <span class="nc">RequestDoneException</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;raised when the request or auth challenge has already been resumed&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="s2">&quot;InterceptedRequest&quot;</span><span class="p">,</span> <span class="s2">&quot;InterceptedAuth&quot;</span><span class="p">]):</span>
        <span class="n">data</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;request or auth challenge with url:&quot;</span><span class="si">{</span><span class="n">data</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">url</span><span class="si">}</span><span class="s1">&quot; has already been resumed&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="n">data</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">request</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="s2">&quot;InterceptedRequest&quot;</span><span class="p">,</span> <span class="s2">&quot;InterceptedAuth&quot;</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the corresponding request or auth challenge&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span></div>



<div class="viewcode-block" id="AuthAlreadyHandledException">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.AuthAlreadyHandledException">[docs]</a>
<span class="k">class</span> <span class="nc">AuthAlreadyHandledException</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;raised when an auth challenge has already been handled by an external application (for example chrome-extension)&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">:</span> <span class="s2">&quot;InterceptedAuth&quot;</span><span class="p">):</span>
        <span class="n">data</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span>
            <span class="sa">f</span><span class="s1">&#39;Auth for url&quot;</span><span class="si">{</span><span class="n">data</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">url</span><span class="si">}</span><span class="s1">&quot; has already been handled by an external application (for example chrome-extension)&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span> <span class="o">=</span> <span class="n">data</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">request</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="s2">&quot;InterceptedAuth&quot;</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the corresponding auth challenge&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span></div>



<div class="viewcode-block" id="RequestStages">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.RequestStages">[docs]</a>
<span class="k">class</span> <span class="nc">RequestStages</span><span class="p">:</span>
    <span class="n">Request</span> <span class="o">=</span> <span class="mi">0</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;stage at request&quot;&quot;&quot;</span>

    <span class="n">Response</span> <span class="o">=</span> <span class="mi">1</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;stage at response&quot;&quot;&quot;</span></div>



<div class="viewcode-block" id="RequestPattern">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.RequestPattern">[docs]</a>
<span class="k">class</span> <span class="nc">RequestPattern</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
    <span class="n">AnyRequest</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;urlPattern&quot;</span><span class="p">:</span> <span class="s2">&quot;*&quot;</span><span class="p">,</span> <span class="s2">&quot;requestStage&quot;</span><span class="p">:</span> <span class="s2">&quot;Request&quot;</span><span class="p">}</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;pattern for any request&quot;&quot;&quot;</span>

    <span class="n">AnyResponse</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;urlPattern&quot;</span><span class="p">:</span> <span class="s2">&quot;*&quot;</span><span class="p">,</span> <span class="s2">&quot;requestStage&quot;</span><span class="p">:</span> <span class="s2">&quot;Response&quot;</span><span class="p">}</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;pattern for any response&quot;&quot;&quot;</span>

<div class="viewcode-block" id="RequestPattern.new">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.RequestPattern.new">[docs]</a>
    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">new</span><span class="p">(</span><span class="n">url_pattern</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
            <span class="n">resource_type</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span>
                <span class="s2">&quot;Document&quot;</span><span class="p">,</span> <span class="s2">&quot;Stylesheet&quot;</span><span class="p">,</span> <span class="s2">&quot;Image&quot;</span><span class="p">,</span> <span class="s2">&quot;Media&quot;</span><span class="p">,</span> <span class="s2">&quot;Font&quot;</span><span class="p">,</span> <span class="s2">&quot;Script&quot;</span><span class="p">,</span> <span class="s2">&quot;TextTrack&quot;</span><span class="p">,</span> <span class="s2">&quot;XHR&quot;</span><span class="p">,</span> <span class="s2">&quot;Fetch&quot;</span><span class="p">,</span> <span class="s2">&quot;Prefetch&quot;</span><span class="p">,</span> <span class="s2">&quot;EventSource&quot;</span><span class="p">,</span> <span class="s2">&quot;WebSocket&quot;</span><span class="p">,</span> <span class="s2">&quot;Manifest&quot;</span><span class="p">,</span> <span class="s2">&quot;SignedExchange&quot;</span><span class="p">,</span> <span class="s2">&quot;Ping&quot;</span><span class="p">,</span> <span class="s2">&quot;CSPViolationReport&quot;</span><span class="p">,</span> <span class="s2">&quot;Preflight&quot;</span><span class="p">,</span> <span class="s2">&quot;Other&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
            <span class="n">request_stage</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span><span class="s2">&quot;Request&quot;</span><span class="p">,</span> <span class="s2">&quot;Response&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        create a new request pattern</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">pattern</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="n">url_pattern</span><span class="p">:</span>
            <span class="n">pattern</span><span class="p">[</span><span class="s2">&quot;urlPattern&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">url_pattern</span>
        <span class="k">if</span> <span class="n">resource_type</span><span class="p">:</span>
            <span class="n">pattern</span><span class="p">[</span><span class="s2">&quot;resourceType&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">resource_type</span>
        <span class="k">if</span> <span class="n">request_stage</span><span class="p">:</span>
            <span class="n">pattern</span><span class="p">[</span><span class="s2">&quot;requestStage&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">request_stage</span></div>
</div>



<div class="viewcode-block" id="Request">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.Request">[docs]</a>
<span class="k">class</span> <span class="nc">Request</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="n">params</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_target</span> <span class="o">=</span> <span class="n">target</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">Target</span><span class="p">,</span> <span class="n">BaseTarget</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the target&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_target</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">params</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">dict</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the parameters from `Network.Request &lt;https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-Request&gt;`_ as a dict&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">url</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Request URL (without fragment).&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">url_fragment</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Fragment of the requested URL starting with hash, if present.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;urlFragment&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;HTTP request method.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;method&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">headers</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Request / response headers as keys / values of JSON object.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;headers&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">post_data</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;HTTP POST request data&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;postData&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">has_post_data</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;True when the request has POST data. Note that postData might still be omitted when this flag is true when the data is too long.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;hasPostData&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">post_data_entries</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;an array of `Network.PostDataEntry &lt;https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-PostDataEntry&gt;`_&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;postDataEntries&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">mixed_content_type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span><span class="s2">&quot;blockable&quot;</span><span class="p">,</span> <span class="s2">&quot;optionally-blockable&quot;</span><span class="p">,</span> <span class="s2">&quot;none&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;The mixed content type of the request.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;mixedContentType&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">initial_priority</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span><span class="s2">&quot;VeryLow&quot;</span><span class="p">,</span> <span class="s2">&quot;Low&quot;</span><span class="p">,</span> <span class="s2">&quot;Medium&quot;</span><span class="p">,</span> <span class="s2">&quot;High&quot;</span><span class="p">,</span> <span class="s2">&quot;VeryHigh&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Priority of the resource request at the time request is sent.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;initialPriority&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">referrer_policy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span><span class="s2">&quot;unsafe-url&quot;</span><span class="p">,</span> <span class="s2">&quot;no-referrer-when-downgrade&quot;</span><span class="p">,</span> <span class="s2">&quot;no-referrer&quot;</span><span class="p">,</span> <span class="s2">&quot;origin&quot;</span><span class="p">,</span> <span class="s2">&quot;origin-when-cross-origin&quot;</span><span class="p">,</span> <span class="s2">&quot;same-origin&quot;</span><span class="p">,</span> <span class="s2">&quot;strict-origin&quot;</span><span class="p">,</span> <span class="s2">&quot;strict-origin-when-cross-origin&quot;</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The referrer policy of the request, as defined in `w3.org/TR/referrer-policy &lt;https://www.w3.org/TR/referrer-policy/&gt;`_</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;referrerPolicy&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_link_preload</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Whether is loaded via link preload.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;isLinkPreload&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">trust_token_params</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">dict</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;see `Network.TrustTokenParams &lt;https://chromedevtools.github.io/devtools-protocol/tot/Network/#type-TrustTokenParams&gt;`_ for reference&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;trustTokenParams&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_same_site</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">bool</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        True if this resource request is considered to be the &#39;same site&#39; as the request corresponding to the main frame.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;isSameSite&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span></div>



<div class="viewcode-block" id="AuthChallenge">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.AuthChallenge">[docs]</a>
<span class="k">class</span> <span class="nc">AuthChallenge</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="n">params</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_target</span> <span class="o">=</span> <span class="n">target</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">Target</span><span class="p">,</span> <span class="n">BaseTarget</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the target&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_target</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">params</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">dict</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the parameters from `Fetch.AuthChallenge &lt;https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#type-AuthChallenge&gt;`_ as a dict&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">source</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span><span class="s2">&quot;Server&quot;</span><span class="p">,</span> <span class="s2">&quot;Proxy&quot;</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Source of the authentication challenge&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;source&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">origin</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;Origin of the challenger.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s2">&quot;origin&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">scheme</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;The authentication scheme used, such as basic or digest&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s2">&quot;scheme&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">realm</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;corresponding value for ``Please enter your username and password for &lt;realm name&gt;:``&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s2">&quot;realm&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span></div>



<div class="viewcode-block" id="InterceptedRequest">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedRequest">[docs]</a>
<span class="k">class</span> <span class="nc">InterceptedRequest</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="n">params</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_target</span> <span class="o">=</span> <span class="n">target</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stage</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_is_redirect</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_body</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timeout</span> <span class="o">=</span> <span class="mi">10</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">Target</span><span class="p">,</span> <span class="n">BaseTarget</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the target&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_target</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">stage</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        one of :func:`RequestStages.Request &lt;selenium_driverless.scripts.network_interceptor.RequestStages.Request&gt;`, :func:`RequestStages.Response &lt;selenium_driverless.scripts.network_interceptor.RequestStages.Response&gt;`</span>
<span class="sd">        0 =&gt; Request</span>
<span class="sd">        1 =&gt; Response</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_status_code</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_error_reason</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_stage</span> <span class="o">=</span> <span class="n">RequestStages</span><span class="o">.</span><span class="n">Response</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_stage</span> <span class="o">=</span> <span class="n">RequestStages</span><span class="o">.</span><span class="n">Request</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_stage</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">is_redirect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        if the response is a redirect (response_status_code in [301, 302, 303, 307, 308])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_redirect</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_status_code</span> <span class="ow">and</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">response_status_code</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">301</span><span class="p">,</span> <span class="mi">302</span><span class="p">,</span> <span class="mi">303</span><span class="p">,</span> <span class="mi">307</span><span class="p">,</span> <span class="mi">308</span><span class="p">]):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_is_redirect</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_is_redirect</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_redirect</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">params</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">dict</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the parameters from `Fetch.requestPaused &lt;https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#event-requestPaused&gt;`_ as a dict&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">frame_id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the ``Page.FrameId`` of the frame that initiated the request&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;frameId&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">request</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Request</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the corresponding request&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;request&quot;</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_request</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;``Fetch.RequestId``&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;requestId&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">resource_type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        ``Network.resourceType``</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;resourceType&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">network_id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        the ``Network.RequestId`` if a corresponding ``Network.requestWillBeSent`` event had been fired</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;networkId&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">response_error_reason</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        response error-reason at response stage</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;responseErrorReason&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">response_headers</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]],</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        the headers at response stage</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;responseHeaders&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">response_status_code</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        the status code at response stage</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;responseStatusCode&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">response_status_text</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        the status text at response stage</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;responseStatusText&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">redirected_id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        ``Fetch.requestId`` reference id to the request that caused the redirect</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;redirectedRequestId&quot;</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">body</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">bytes</span><span class="p">,</span> <span class="kc">None</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;**async**</span>
<span class="sd">        get the response body for a request</span>

<span class="sd">        .. note::</span>
<span class="sd">            this uses network and might take some time</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_body</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
            <span class="n">body</span> <span class="o">=</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.getResponseBody&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s2">&quot;requestId&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">},</span>
                                                     <span class="n">timeout</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">timeout</span><span class="p">)</span>
            <span class="n">body</span> <span class="o">=</span> <span class="n">body</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">body</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_body</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">body</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_body</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_body</span>

<div class="viewcode-block" id="InterceptedRequest.bypass_browser">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedRequest.bypass_browser">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">bypass_browser</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">auth</span><span class="p">:</span> <span class="n">aiohttp</span><span class="o">.</span><span class="n">BasicAuth</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">allow_redirects</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">compress</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                             <span class="n">proxy</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">proxy_auth</span><span class="p">:</span> <span class="n">aiohttp</span><span class="o">.</span><span class="n">BasicAuth</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        bypass browser by making the request externally</span>

<span class="sd">        .. warning::</span>
<span class="sd">            this method does not change the TLS fingerprint accordingly and is technically detectable</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">RequestDoneException</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">async</span> <span class="k">with</span> <span class="n">aiohttp</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">method</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">url</span><span class="p">,</span>
                                       <span class="n">data</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">post_data</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">headers</span><span class="p">,</span> <span class="n">auth</span><span class="o">=</span><span class="n">auth</span><span class="p">,</span>
                                       <span class="n">allow_redirects</span><span class="o">=</span><span class="n">allow_redirects</span><span class="p">,</span> <span class="n">compress</span><span class="o">=</span><span class="n">compress</span><span class="p">,</span>
                                       <span class="n">proxy</span><span class="o">=</span><span class="n">proxy</span><span class="p">,</span> <span class="n">proxy_auth</span><span class="o">=</span><span class="n">proxy_auth</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="n">timeout</span><span class="p">)</span> <span class="k">as</span> <span class="n">resp</span><span class="p">:</span>
                <span class="n">body</span> <span class="o">=</span> <span class="k">await</span> <span class="n">resp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
                <span class="n">response_headers</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">resp</span><span class="o">.</span><span class="n">headers</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="n">response_headers</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s2">&quot;name&quot;</span><span class="p">:</span> <span class="n">name</span><span class="p">,</span> <span class="s2">&quot;value&quot;</span><span class="p">:</span> <span class="n">value</span><span class="p">})</span>
                <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">fulfill</span><span class="p">(</span><span class="n">response_code</span><span class="o">=</span><span class="n">resp</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="n">body</span><span class="p">,</span>
                                   <span class="n">response_headers</span><span class="o">=</span><span class="n">response_headers</span><span class="p">,</span> <span class="n">response_phrase</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span></div>


<div class="viewcode-block" id="InterceptedRequest.continue_request">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedRequest.continue_request">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">continue_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">headers</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">method</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                               <span class="n">post_data</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">url</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                               <span class="n">intercept_response</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        continue the request.</span>
<span class="sd">        You might use :func:`InterceptedRequest.fulfill &lt;selenium_driverless.scripts.network_interceptor.InterceptedRequest.fulfill&gt;`</span>
<span class="sd">        to provide a response body</span>

<span class="sd">        :param headers: array of {&quot;name&quot;:name, &quot;value&quot;:value},  mind header order</span>
<span class="sd">        :param method: the method of the request</span>
<span class="sd">        :param post_data: the binary post data to provide</span>
<span class="sd">        :param url: the url to continue the request with, change not observable by the page</span>
<span class="sd">        :param intercept_response: overrides response interception behavior for this request</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">RequestDoneException</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;requestId&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">}</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">post_data</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">post_data</span> <span class="o">=</span> <span class="n">post_data</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">post_data</span><span class="p">:</span>
            <span class="n">post_data</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">post_data</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;ascii&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">headers</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;headers&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">headers</span>
        <span class="k">if</span> <span class="n">method</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;method&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">method</span>
        <span class="k">if</span> <span class="n">post_data</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;postData&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">post_data</span>
        <span class="k">if</span> <span class="n">url</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;url&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">url</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">intercept_response</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">):</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;interceptResponse&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">intercept_response</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.continueRequest&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">CDPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="o">-</span><span class="mi">32602</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="s1">&#39;Invalid InterceptionId.&#39;</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">e</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">True</span></div>


<div class="viewcode-block" id="InterceptedRequest.continue_response">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedRequest.continue_response">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">continue_response</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response_headers</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                                <span class="n">binary_response_headers</span><span class="p">:</span> <span class="nb">bytes</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                                <span class="n">response_code</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">response_phrase</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        continue the response.</span>
<span class="sd">        You might use :func:`By &lt;selenium_driverless.scripts.network_interceptor.InterceptedRequest.fulfill&gt;`</span>
<span class="sd">        to provide a response body</span>

<span class="sd">        :param response_code: response code</span>
<span class="sd">        :param binary_response_headers:  headers as a \0-separated series of name: value pairs, treated as base64 encode if a string is passed,</span>
<span class="sd">        :param response_headers: array of {&quot;name&quot;:name, &quot;value&quot;:value},  mind header order</span>
<span class="sd">        :param response_phrase: response phrase (``&quot;OK&quot;`` for ``response_code=200``)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">RequestDoneException</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">binary_response_headers</span><span class="p">:</span>
            <span class="n">binary_response_headers</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">binary_response_headers</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;ascii&quot;</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;requestId&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">}</span>
        <span class="k">if</span> <span class="n">response_headers</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;responseHeaders&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">response_headers</span>
        <span class="k">if</span> <span class="n">binary_response_headers</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;binaryResponseHeaders&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">binary_response_headers</span>
        <span class="k">if</span> <span class="n">response_code</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;responseCode&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">response_code</span>
        <span class="k">if</span> <span class="n">response_phrase</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;responsePhrase&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">response_phrase</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.continueResponse&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">CDPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="o">-</span><span class="mi">32602</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="s1">&#39;Invalid InterceptionId.&#39;</span><span class="p">):</span>
                <span class="c1"># request cancelled due to reload for example</span>
                <span class="k">raise</span> <span class="n">e</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">True</span></div>


<div class="viewcode-block" id="InterceptedRequest.resume">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedRequest.resume">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">resume</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        continue the request or response</span>
<span class="sd">        doesn&#39;t raise id the request is canceled or already done</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">continue_request</span><span class="p">()</span>
            <span class="k">except</span> <span class="n">websockets</span><span class="o">.</span><span class="n">ConnectionClosedError</span><span class="p">:</span>
                <span class="k">pass</span></div>


<div class="viewcode-block" id="InterceptedRequest.fail_request">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedRequest.fail_request">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">fail_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">error_reason</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span>
            <span class="s2">&quot;Failed&quot;</span><span class="p">,</span> <span class="s2">&quot;Aborted&quot;</span><span class="p">,</span> <span class="s2">&quot;TimedOut&quot;</span><span class="p">,</span> <span class="s2">&quot;AccessDenied&quot;</span><span class="p">,</span> <span class="s2">&quot;ConnectionClosed&quot;</span><span class="p">,</span> <span class="s2">&quot;ConnectionReset&quot;</span><span class="p">,</span> <span class="s2">&quot;ConnectionRefused&quot;</span><span class="p">,</span> <span class="s2">&quot;ConnectionAborted&quot;</span><span class="p">,</span> <span class="s2">&quot;ConnectionFailed&quot;</span><span class="p">,</span> <span class="s2">&quot;NameNotResolved&quot;</span><span class="p">,</span> <span class="s2">&quot;InternetDisconnected&quot;</span><span class="p">,</span> <span class="s2">&quot;AddressUnreachable&quot;</span><span class="p">,</span> <span class="s2">&quot;BlockedByClient&quot;</span><span class="p">,</span> <span class="s2">&quot;BlockedByResponse&quot;</span><span class="p">]):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        fail the request or response</span>

<span class="sd">        :param error_reason: the specified reason for the request failing</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">RequestDoneException</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;requestId&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">}</span>
        <span class="k">if</span> <span class="n">error_reason</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;errorReason&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">error_reason</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.failRequest&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">CDPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="o">-</span><span class="mi">32602</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="s1">&#39;Invalid InterceptionId.&#39;</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">e</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">True</span></div>


<div class="viewcode-block" id="InterceptedRequest.fulfill">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedRequest.fulfill">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">fulfill</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response_code</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">binary_response_headers</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                      <span class="n">body</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                      <span class="n">response_headers</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">str</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">response_phrase</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        fulfill the request or response</span>

<span class="sd">        :param response_code: response code</span>
<span class="sd">        :param body: the response body</span>
<span class="sd">        :param binary_response_headers:  headers as a \0-separated series of name: value pairs, treated as base64 encode if a string is passed,</span>
<span class="sd">        :param response_headers: array of {&quot;name&quot;:name, &quot;value&quot;:value},  mind header order</span>
<span class="sd">        :param response_phrase: response phrase (``&quot;OK&quot;`` for ``response_code=200``)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">RequestDoneException</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;requestId&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">}</span>

        <span class="k">if</span> <span class="n">response_headers</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_headers</span><span class="p">:</span>
                <span class="n">response_headers</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_headers</span>
        <span class="k">if</span> <span class="n">response_code</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_status_code</span><span class="p">:</span>
                <span class="n">response_code</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_status_code</span>
        <span class="k">if</span> <span class="n">response_phrase</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_status_text</span> <span class="o">!=</span> <span class="s2">&quot;&quot;</span><span class="p">:</span>
                <span class="c1"># can&#39;t be empty</span>
                <span class="n">response_phrase</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">response_status_text</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="n">body</span> <span class="o">=</span> <span class="n">body</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">body</span><span class="p">:</span>
            <span class="n">body</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">body</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;ascii&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">binary_response_headers</span><span class="p">,</span> <span class="nb">bytes</span><span class="p">):</span>
            <span class="n">binary_response_headers</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="n">binary_response_headers</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s2">&quot;ascii&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">response_code</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;responseCode&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">response_code</span>
        <span class="k">if</span> <span class="n">binary_response_headers</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;binaryResponseHeaders&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">binary_response_headers</span>
        <span class="k">if</span> <span class="n">body</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;body&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">body</span>
        <span class="k">if</span> <span class="n">response_headers</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;responseHeaders&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">response_headers</span>
        <span class="k">if</span> <span class="n">response_phrase</span><span class="p">:</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;responsePhrase&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">response_phrase</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.fulfillRequest&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">CDPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="o">-</span><span class="mi">32602</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="s1">&#39;Invalid InterceptionId.&#39;</span><span class="p">):</span>
                <span class="k">raise</span> <span class="n">e</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">True</span></div>


    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span></div>



<div class="viewcode-block" id="InterceptedAuth">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedAuth">[docs]</a>
<span class="k">class</span> <span class="nc">InterceptedAuth</span><span class="p">:</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_params</span> <span class="o">=</span> <span class="n">params</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_target</span> <span class="o">=</span> <span class="n">target</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_stage</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_is_redirect</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_body</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_auth_challenge</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timeout</span> <span class="o">=</span> <span class="mi">10</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">request</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Request</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the Request, for which auth is required&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;request&quot;</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_request</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the ``Fetch.RequestId``&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s2">&quot;requestId&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">frame_id</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the ``Page.FrameId`` from which the request has been initiated&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;frameId&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">params</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">dict</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the parameters from `Fetch.authRequired &lt;https://chromedevtools.github.io/devtools-protocol/tot/Fetch/#event-authRequired&gt;`_ as a dict&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">Target</span><span class="p">,</span> <span class="n">BaseTarget</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the target&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_target</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">resource_type</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        the `Network.ResourceType`</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;resourceType&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">auth_challenge</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">AuthChallenge</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        the AuthChallenge</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_auth_challenge</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_auth_challenge</span> <span class="o">=</span> <span class="n">AuthChallenge</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_params</span><span class="p">[</span><span class="s2">&quot;request&quot;</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_auth_challenge</span>

<div class="viewcode-block" id="InterceptedAuth.continue_auth">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedAuth.continue_auth">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">continue_auth</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">response</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Literal</span><span class="p">[</span><span class="s2">&quot;Default&quot;</span><span class="p">,</span> <span class="s2">&quot;CancelAuth&quot;</span><span class="p">,</span> <span class="s2">&quot;ProvideCredentials&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Default&quot;</span><span class="p">,</span>
                            <span class="n">username</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">password</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        continue the auth</span>

<span class="sd">        :param response: &quot;Default&quot; by default, automatically set to &quot;ProvideCredentials&quot;, if username or password specified</span>
<span class="sd">        :param username: username for the auth</span>
<span class="sd">        :param password: password for the auth</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">RequestDoneException</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">username</span> <span class="ow">or</span> <span class="n">password</span><span class="p">:</span>
            <span class="n">response</span> <span class="o">=</span> <span class="s2">&quot;ProvideCredentials&quot;</span>
        <span class="n">challenge_response</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;response&quot;</span><span class="p">:</span> <span class="n">response</span><span class="p">}</span>
        <span class="k">if</span> <span class="n">username</span><span class="p">:</span>
            <span class="n">challenge_response</span><span class="p">[</span><span class="s2">&quot;username&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">username</span>
        <span class="k">if</span> <span class="n">password</span><span class="p">:</span>
            <span class="n">challenge_response</span><span class="p">[</span><span class="s2">&quot;password&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">password</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.continueWithAuth&quot;</span><span class="p">,</span>
                                              <span class="p">{</span><span class="s2">&quot;requestId&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="s2">&quot;authChallengeResponse&quot;</span><span class="p">:</span> <span class="n">challenge_response</span><span class="p">},</span> <span class="n">timeout</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">timeout</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_done</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">except</span> <span class="n">CDPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="o">-</span><span class="mi">32000</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="s1">&#39;Invalid state for continueInterceptedRequest&#39;</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">AuthAlreadyHandledException</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">e</span></div>


<div class="viewcode-block" id="InterceptedAuth.resume">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedAuth.resume">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">resume</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        continue the auth</span>
<span class="sd">        doesn&#39;t raise id the request is canceled or already done</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_done</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">continue_auth</span><span class="p">()</span>
            <span class="k">except</span> <span class="p">(</span><span class="n">AuthAlreadyHandledException</span><span class="p">,</span> <span class="n">websockets</span><span class="o">.</span><span class="n">ConnectionClosedError</span><span class="p">):</span>
                <span class="k">pass</span></div>


<div class="viewcode-block" id="InterceptedAuth.cancel">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.InterceptedAuth.cancel">[docs]</a>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">cancel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        cancel the auth</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">continue_auth</span><span class="p">(</span><span class="n">response</span><span class="o">=</span><span class="s2">&quot;CancelAuth&quot;</span><span class="p">)</span></div>


    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="o">.</span><span class="fm">__repr__</span><span class="p">()</span></div>



<span class="n">RequestCallbackType</span> <span class="o">=</span> <span class="n">typing</span><span class="o">.</span><span class="n">Callable</span><span class="p">[[</span><span class="n">InterceptedRequest</span><span class="p">],</span> <span class="n">typing</span><span class="o">.</span><span class="n">Awaitable</span><span class="p">[</span><span class="kc">None</span><span class="p">]]</span>
<span class="n">AuthCallbackType</span> <span class="o">=</span> <span class="n">typing</span><span class="o">.</span><span class="n">Callable</span><span class="p">[[</span><span class="n">InterceptedRequest</span><span class="p">],</span> <span class="n">typing</span><span class="o">.</span><span class="n">Awaitable</span><span class="p">[</span><span class="kc">None</span><span class="p">]]</span>


<div class="viewcode-block" id="NetworkInterceptor">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.NetworkInterceptor">[docs]</a>
<span class="k">class</span> <span class="nc">NetworkInterceptor</span><span class="p">:</span>
    <span class="n">on_request</span><span class="p">:</span> <span class="n">RequestCallbackType</span>
    <span class="n">on_response</span><span class="p">:</span> <span class="n">RequestCallbackType</span>
    <span class="n">on_auth</span><span class="p">:</span> <span class="n">AuthCallbackType</span>

<div class="viewcode-block" id="NetworkInterceptor.__init__">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.NetworkInterceptor.__init__">[docs]</a>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">target</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">Chrome</span><span class="p">,</span> <span class="n">Target</span><span class="p">],</span> <span class="n">on_request</span><span class="p">:</span> <span class="n">RequestCallbackType</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                 <span class="n">on_response</span><span class="p">:</span> <span class="n">RequestCallbackType</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">on_auth</span><span class="p">:</span> <span class="n">AuthCallbackType</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
                 <span class="n">patterns</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">PatternsType</span><span class="p">,</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="n">RequestPattern</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span> <span class="n">intercept_auth</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
                 <span class="n">bypass_service_workers</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param target: the Target or Driver, on which requests get intercepted</span>
<span class="sd">        :param on_request: onRequest callback</span>
<span class="sd">        :param on_response: onResponse callback</span>
<span class="sd">        :param on_auth: onAuth callback</span>
<span class="sd">        :param patterns: the request patterns to intercept</span>
<span class="sd">        :param intercept_auth: whether to intercept authentification</span>
<span class="sd">        :param bypass_service_workers: whether to bypass service workers for a single Target</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">patterns</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">patterns</span> <span class="o">=</span> <span class="p">[</span><span class="n">RequestPattern</span><span class="o">.</span><span class="n">AnyRequest</span><span class="p">,</span> <span class="n">RequestPattern</span><span class="o">.</span><span class="n">AnyResponse</span><span class="p">]</span>

        <span class="n">_patters</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">pattern</span> <span class="ow">in</span> <span class="n">patterns</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">RequestPattern</span><span class="p">):</span>
                <span class="n">pattern</span> <span class="o">=</span> <span class="n">pattern</span><span class="o">.</span><span class="n">value</span>
            <span class="n">_patters</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_iter_callbacks</span><span class="p">:</span> <span class="n">typing</span><span class="o">.</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">typing</span><span class="o">.</span><span class="n">List</span><span class="p">[</span><span class="n">asyncio</span><span class="o">.</span><span class="n">Future</span><span class="p">]]</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">target</span><span class="p">,</span> <span class="n">Chrome</span><span class="p">):</span>
            <span class="n">driver</span> <span class="o">=</span> <span class="n">target</span>
            <span class="n">target</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">base_target</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># noinspection PyProtectedMember</span>
            <span class="n">driver</span> <span class="o">=</span> <span class="n">target</span><span class="o">.</span><span class="n">_driver</span>

        <span class="c1"># noinspection PyUnusedLocal</span>
        <span class="k">async</span> <span class="k">def</span> <span class="nf">blank_callback</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
            <span class="k">pass</span>

        <span class="k">if</span> <span class="n">on_request</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">on_request</span> <span class="o">=</span> <span class="n">blank_callback</span>
        <span class="k">if</span> <span class="n">on_response</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">on_response</span> <span class="o">=</span> <span class="n">blank_callback</span>
        <span class="k">if</span> <span class="n">on_auth</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">on_auth</span> <span class="o">=</span> <span class="n">blank_callback</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_driver</span> <span class="o">=</span> <span class="n">driver</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_started</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_service_workers</span> <span class="o">=</span> <span class="n">bypass_service_workers</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">on_request</span> <span class="o">=</span> <span class="n">on_request</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">on_response</span> <span class="o">=</span> <span class="n">on_response</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">on_auth</span> <span class="o">=</span> <span class="n">on_auth</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_target</span> <span class="o">=</span> <span class="n">target</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_patterns</span> <span class="o">=</span> <span class="n">_patters</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_intercept_auth</span> <span class="o">=</span> <span class="n">intercept_auth</span></div>


    <span class="k">async</span> <span class="k">def</span> <span class="fm">__aenter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_started</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.enable&quot;</span><span class="p">,</span>
                                              <span class="n">cmd_args</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;patterns&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_patterns</span><span class="p">,</span>
                                                        <span class="s2">&quot;handleAuthRequests&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_intercept_auth</span><span class="p">})</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">add_cdp_listener</span><span class="p">(</span><span class="s2">&quot;Fetch.authRequired&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_paused_handler</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_service_workers</span><span class="p">:</span>
                <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Network.setBypassServiceWorker&quot;</span><span class="p">,</span>
                                                  <span class="p">{</span><span class="s2">&quot;bypass&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_service_workers</span><span class="p">})</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">add_cdp_listener</span><span class="p">(</span><span class="s2">&quot;Fetch.requestPaused&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_paused_handler</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_started</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">async</span> <span class="k">def</span> <span class="fm">__aexit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_val</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_bypass_service_workers</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">execute_cdp_cmd</span><span class="p">(</span><span class="s2">&quot;Fetch.disable&quot;</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">CDPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">code</span> <span class="o">==</span> <span class="o">-</span><span class="mi">32000</span> <span class="ow">and</span> <span class="n">e</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="s1">&#39;Fetch domain is not enabled&#39;</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="n">e</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">remove_cdp_listener</span><span class="p">(</span><span class="s2">&quot;Fetch.requestPaused&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_paused_handler</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">pass</span>  <span class="c1"># ValueError: list.remove(x): x not in list</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">remove_cdp_listener</span><span class="p">(</span><span class="s2">&quot;Fetch.authRequired&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_paused_handler</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">pass</span>

    <span class="k">async</span> <span class="k">def</span> <span class="nf">_paused_handler</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">:</span> <span class="nb">dict</span><span class="p">):</span>
        <span class="k">if</span> <span class="s2">&quot;authChallenge&quot;</span> <span class="ow">in</span> <span class="n">params</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="n">request</span> <span class="o">=</span> <span class="n">InterceptedAuth</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">request</span> <span class="o">=</span> <span class="n">InterceptedRequest</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>
        <span class="n">coro</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">_id</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_iter_callbacks</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
            <span class="n">fut</span><span class="p">,</span> <span class="n">done</span> <span class="o">=</span> <span class="n">val</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">fut</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">InvalidStateError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">coro</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">done</span><span class="p">)</span>
            <span class="k">finally</span><span class="p">:</span>
                <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_iter_callbacks</span><span class="p">[</span><span class="n">_id</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">coro</span><span class="p">:</span>
            <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span><span class="o">*</span><span class="n">coro</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">InterceptedRequest</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">stage</span> <span class="o">==</span> <span class="n">RequestStages</span><span class="o">.</span><span class="n">Response</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_response</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">await</span> <span class="n">request</span><span class="o">.</span><span class="n">resume</span><span class="p">()</span>
                    <span class="k">raise</span> <span class="n">e</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_request</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">await</span> <span class="n">request</span><span class="o">.</span><span class="n">resume</span><span class="p">()</span>
                    <span class="k">raise</span> <span class="n">e</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">on_auth</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">await</span> <span class="n">request</span><span class="o">.</span><span class="n">resume</span><span class="p">()</span>

<div class="viewcode-block" id="NetworkInterceptor.__aiter__">
<a class="viewcode-back" href="../../../../api/RequestInterception/#selenium_driverless.scripts.network_interceptor.NetworkInterceptor.__aiter__">[docs]</a>
    <span class="k">def</span> <span class="fm">__aiter__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">AsyncIterator</span><span class="p">[</span><span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">InterceptedRequest</span><span class="p">,</span> <span class="n">InterceptedAuth</span><span class="p">]]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        iterate using ``async for`` over requests</span>

<span class="sd">        **Example**</span>

<span class="sd">        .. code-block:: Python</span>

<span class="sd">            from selenium_driverless.scripts.network_interceptor import InterceptedRequest, RequestStages</span>

<span class="sd">            # ... some code</span>
<span class="sd">            async for data in interceptor:</span>
<span class="sd">                url = data.request.url</span>
<span class="sd">                if isinstance(data, InterceptedRequest):</span>
<span class="sd">                    data.stage == RequestStages.Request:</span>
<span class="sd">                        print(&quot;Request:&quot;+url)</span>
<span class="sd">                    else:</span>
<span class="sd">                        print(&quot;Response:&quot;+url)</span>
<span class="sd">                else:</span>
<span class="sd">                    print(&quot;Authentification:&quot;+url)</span>


<span class="sd">        .. warning::</span>
<span class="sd">            iterations should virtually take zero time, you might use ``asyncio.ensure_future`` where possible</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">async</span> <span class="k">def</span> <span class="nf">_iter</span><span class="p">():</span>
            <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                <span class="n">fut</span><span class="p">,</span> <span class="n">done</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Future</span><span class="p">(),</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Future</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_iter_callbacks</span><span class="p">[</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">()</span><span class="o">.</span><span class="n">hex</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">fut</span><span class="p">,</span> <span class="n">done</span><span class="p">]</span>
                <span class="n">res</span> <span class="o">=</span> <span class="k">await</span> <span class="n">fut</span>
                <span class="k">yield</span> <span class="n">res</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">done</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="k">except</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">InvalidStateError</span><span class="p">:</span>
                    <span class="k">pass</span>

        <span class="k">return</span> <span class="n">_iter</span><span class="p">()</span></div>


    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">patterns</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">PatternsType</span><span class="p">:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;patters to intercept&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_patterns</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">typing</span><span class="o">.</span><span class="n">Union</span><span class="p">[</span><span class="n">Target</span><span class="p">,</span> <span class="n">BaseTarget</span><span class="p">]:</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;the Target, on which requests get intercepted&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_target</span></div>

</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2024, Aurin Aegerter (aka Steve, kaliiiiiiiiii).</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>